package struct;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import struct.enums.State;
import struct.enums.ValueType;

import java.util.List;

public class ActorInfo {
    public static final String ACTOR_NAME_FIELD = "actor_name";
    private static final String VALUE_TYPE_FIELD = "value_type";
    private static final String ACTOR_STATE_FIELD = "state";
    private static final String ACTOR_APPS_FIELD = "apps";

    public String name;
    public ValueType type;
    public State state;
    public List<String> apps;
    private JSONObject jo;

    public ActorInfo(String name, ValueType type, State state, List<String> apps) {
        this.state = state;
        this.type = type;
        this.name = name;
        this.apps = apps;
    }

    public ActorInfo(JSONObject obj) {
        this.jo = obj;
        this.name = obj.getString(ACTOR_NAME_FIELD);
        this.type = ValueType.fromString(obj.getString(VALUE_TYPE_FIELD));
        this.state = State.fromString(obj.getString(ACTOR_STATE_FIELD));
        JSONArray ja = obj.getJSONArray(ACTOR_APPS_FIELD);
        apps = ja.toJavaList(String.class);
    }

    @Override
    public String toString() {
        if (jo != null) return jo.toJSONString();
        jo = new JSONObject();
        jo.put(ACTOR_NAME_FIELD, name);
        jo.put(VALUE_TYPE_FIELD, type.toString());
        jo.put(ACTOR_STATE_FIELD, state.toString());
        JSONArray ja = new JSONArray();
        ja.addAll(apps);
        jo.put(ACTOR_APPS_FIELD, ja);
        return jo.toJSONString();
    }
}
